import 'package:flutter/material.dart';
import 'package:connectivity/connectivity.dart';
class CheckNetworkPage extends StatefulWidget {
  CheckNetworkPage({Key key}) : super(key: key);

  _CheckNetworkPageState createState() => _CheckNetworkPageState();
}

class _CheckNetworkPageState extends State<CheckNetworkPage> {
  var subscription;
  String _state;
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    subscription = Connectivity().onConnectivityChanged.listen((ConnectivityResult result) {
      print(result);
      if(result == ConnectivityResult.wifi){
         setState(() {
           _state = '处于wifi';
         }); 
      }else if(result == ConnectivityResult.mobile){
         setState(() {
           _state = '处于手机网络';
         }); 
      }else{
        setState(() {
           _state = '没有网络';
        });
      }
    });
  }
  @override
  dispose() {
    super.dispose();
    subscription.cancel();
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("检查网络状态"),
      ),
      body: Center(
        child: Text('${_state}'),
      ),
    );
  }
}